<template>
  <div class="main">
    <a-card class="w-full h-full">
      <SearchGroup
        :labelOption="labelOption"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
        :searchOption="filterOption"
      />

      <a-table
        :dataSource="list"
        :columns="createColumns()"
        bordered
        bodyCell
        :loading="loading"
        :scroll="{ x: 1450 }"
        size="small"
        :pagination="false"
        :row-class-name="
          record => {
            if (record.pyZt == 'PY_DPY') return 'bg-red-200'
            if (record.pyZt == 'PY_YPY') return 'bg-green-200'
            return 'bg-orange-200'
          }
        "
      >
        <template #bodyCell="{ column, index }">
          <template v-if="column.key == 'index'">
            <span>
              {{ (curPage - 1) * pageSize + (index + 1) }}
            </span>
          </template>
        </template>
      </a-table>
      <a-pagination
        :show-total="total => `共 ${total} 条`"
        :show-size-changer="false"
        show-quick-jumper
        v-model:current="curPage"
        v-model:pageSize="pageSize"
        :total="total"
      />
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useList } from '/@/hooks/web/useList'
import { getFindKmPyztPaper, getAcountmdm } from '/@/api/examinationAudit'
import { getKmSimpAlleList } from '/@/api/organization/subjectManage'
import { getPySzListByKmdm } from '/@/api/teacher'

import { createColumns } from './data'
import { selectKmpyjdOption } from '/@/views/home/index/index'
import { useUserStore } from '/@/store/modules/user'
import { useMessage } from '/@/hooks/web/useMessage'

const userStore = useUserStore()
const userInfo: any = computed(() => userStore.getUserInfo)
const { createMessage } = useMessage()
const kmListOption = ref<any>([])
const stOptionList = ref<any>([])

const labelOption = ref([
  {
    name: '评阅阶段',
    key: 'zpDataFlag',
    labelWidth: 80,
    selectOption: selectKmpyjdOption,
    type: 'Radio',
    inputWidth: 180,
    isNotReset: true,
  },
  {
    labelWidth: 50,
    key: 'kmdm',
    disabled: userInfo?.value?.manageKmdm && userInfo.value?.manageKmdm != '0',
    type: 'Select',
    name: '科目',
    selectOption: kmListOption,
    fieldNames: { label: 'kmmc', value: 'kmdm' },
  },
  {
    labelWidth: 50,
    key: 'stId',
    type: 'Select',
    name: '试题',
    selectOption: stOptionList,
    fieldNames: { label: 'tmbt', value: 'stId' },
  },
  {
    name: '评阅状态',
    key: 'pyZt',
    labelWidth: 80,
    selectOption: [
      { label: '全部', value: '' },
      { label: '待评阅', value: 'PY_DPY' },
      { label: '评阅中', value: 'PY_PYZ' },
      { label: '异常待处理', value: 'YC_DCL' },
      { label: '异常处理中', value: 'YC_CLZ' },
      { label: '已评阅', value: 'PY_YPY' },
      { label: '待重评', value: 'PY_DCP' },
    ],
    inputWidth: 180,
  },
  {
    name: '是否是复评题',
    key: 'fpFlag',
    labelWidth: 100,
    selectOption: [
      { label: '全部', value: '' },
      { label: '是', value: 'PY_DPY' },
      { label: '否', value: 'PY_PYZ' },
    ],
    inputWidth: 180,
  },
])

function handleSearch() {
  if (!filterOption.value['kmdm'] || !filterOption.value['zpDataFlag']) {
    list.value = []
    if (
      filterOption.value['kmdm'] ||
      filterOption.value['zpDataFlag'] ||
      filterOption.value['pyZt']
    ) {
      createMessage.error('请选择科目和评阅阶段')
    }

    return
  }
  getKmStList()

  loadData()
}



function handleReset() {
  reset()

  list.value = []
}

const {
  reset,
  loadData,
  filterOption,
  curPage,
  total,
  pageSize,
  list,
  loading,
  defaultFilterOption,
} = useList(getFindKmPyztPaper, {
  immediate: false,
})


watch(
  () => [filterOption.value['kmdm'], filterOption.value['zpDataFlag']],
  ([valKmdm, valZpFlag], [oldKmdm, oldZpFlag]) => {
    filterOption.value.stId = null
    stOptionList.value = []
    if (valKmdm && valKmdm != '0' && valZpFlag && (oldKmdm != valKmdm || oldZpFlag != valZpFlag)) {
      getKmStList()
    }
  }
)

async function getKmpyJd(kmdm, isDefault = false) {
  if (!kmdm || kmdm === '0') return
  const { code, data } = await getAcountmdm({ kmdm })
  if (code == 200) {
    !defaultFilterOption['zpDataFlag'] &&
      (defaultFilterOption['zpDataFlag'] = filterOption.value['zpDataFlag'] =
        data.kmpyjd === 'FORMAL' ? 'true' : 'false')
    isDefault &&
      (kmListOption.value = [
        {
          kmmc: `${data.kmdm}(${data.kmmc})`,
          kmdm: data.kmdm,
        },
      ])
  }
}

async function getKmList() {
  let { code, data } = await getKmSimpAlleList({
    pageSize: 0,
  })
  if (code == 200) {
    data.forEach(item => (item.kmmc = `${item.kmdm}(${item.kmmc})`))
    kmListOption.value = data.sort((a, b) => a.kmdm - b.kmdm)
  }
}

async function getKmStList() {
  let { code, data } = await getPySzListByKmdm({
    pageSize: 0,
    kmdm: filterOption.value.kmdm,
    zpDataFlag: filterOption.value['zpDataFlag'],
  })
  if (code == 200) {
    data.forEach(item => (item.tmbt = `${item.tmbh}${item.tmbt || item.tmlx}`))
    stOptionList.value = data || []
  }
}

onMounted(async () => {
  if (userInfo?.value?.manageKmdm && userInfo.value?.manageKmdm != '0') {
    await getKmpyJd(userInfo.value.manageKmdm, true)
    defaultFilterOption['kmdm'] = filterOption.value['kmdm'] = userInfo.value?.manageKmdm
  } else {
    getKmList()
  }
})
</script>

<style lang="less"></style>
